package com.weblog.search.listener;

import com.weblog.search.service.ArticleService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;

/**
 * @author MaoLin Wang
 * @date 2019/10/1917:35
 */
@Component
public class ArticleListener {


    @Autowired
    private ArticleService articleService;

    /**
     * 修改或发布文章
     * @param aid
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @RabbitListener(bindings = @QueueBinding(
        value = @Queue(value = "WEBLOG.ARTICLE.SAVE.QUEUE",durable = "true"),
        exchange = @Exchange(value = "WEBLOG.ARTICLE.EXCHANGE",ignoreDeclarationExceptions ="true",type = ExchangeTypes.TOPIC),
        key = {"article.publish","article.update"}
    ))
    public void save(String aid) throws InvocationTargetException, IllegalAccessException {
        if("".equals(aid)){
            return;
        }
        articleService.save(aid);
    }

    /**
     * 删除文章
     * @param aid
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "WEBLOG.ARTICLE.DELETE.QUEUE",durable = "true"),
            exchange = @Exchange(value = "WEBLOG.ARTICLE.EXCHANGE",ignoreDeclarationExceptions ="true",type = ExchangeTypes.TOPIC),
            key = {"article.delete"}
    ))
    public void delete(String aid)  {
        if("".equals(aid)){
            return;
        }
        articleService.delete(aid);
    }

    /**
     * 后台软删除文章
     * @param aid
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "WEBLOG.ARTICLE.BLACK.QUEUE",durable = "true"),
            exchange = @Exchange(value = "WEBLOG.ARTICLE.EXCHANGE",ignoreDeclarationExceptions ="true",type = ExchangeTypes.TOPIC),
            key = {"article.black"}
    ))
    public void black(String aid){
        if("".equals(aid)){
            return;
        }
        articleService.black(aid);
    }

    /**
     * 点赞
     * @param aid
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "WEBLOG.ARTICLE.THUMBUP.QUEUE",durable = "true"),
            exchange = @Exchange(value = "WEBLOG.ARTICLE.EXCHANGE",ignoreDeclarationExceptions ="true",type = ExchangeTypes.TOPIC),
            key = {"article.thumbup"}
    ))
    public void thumbup(String aid){
        if("".equals(aid)){
            return;
        }
        articleService.thumbup(aid);
    }

    /**
     * 增加评论
     * @param aid
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "WEBLOG.ARTICLE.COMMENT.QUEUE",durable = "true"),
            exchange = @Exchange(value = "WEBLOG.ARTICLE.EXCHANGE",ignoreDeclarationExceptions ="true",type = ExchangeTypes.TOPIC),
            key = {"article.comment"}
    ))
    public void comment(String aid)  {
        if("".equals(aid)){
            return;
        }
        articleService.comment(aid);
    }

}
